import hashlib
import shutil
import sys
import os

def calculate_sha256(file_path):
    sha256_hash = hashlib.sha256()
    with open(file_path, "rb") as file:
        for chunk in iter(lambda: file.read(4096), b""):
            sha256_hash.update(chunk)
    return sha256_hash.digest()

def copy_file_with_sha256(source_file_path, destination_file_path):
    shutil.copy2(source_file_path, destination_file_path)

    sha256_value = calculate_sha256(source_file_path)
    sha256_le = int.from_bytes(sha256_value, byteorder='big').to_bytes(32, byteorder='little')

    with open(destination_file_path, "ab") as file:
        file.write(sha256_le)
        #file.write(b'\x88\x77\x66\x55\x44\x33\x22\x11')
        file.write(b'\x20\x00\x00\x00\xCD\x29\xB7\x06')
        file.write(b'\x01\x00\x00\x00')


if len(sys.argv) < 2:
    print("请指定输入文件名")
    sys.exit(1)

source_file = sys.argv[1]
output_file_name = os.path.splitext(source_file)[0] + "_OTA.gbl"


copy_file_with_sha256(source_file, output_file_name)

print("操作已完成。生成的文件名为:", output_file_name)